﻿    using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DTTB.Common;
using DTTB.Domain;

namespace DTTB.DAO
{
    public class RolesDao : AccessData
    {
        //Lấy tất cả Role trong trạng thái Enable và không đánh dấu Delete
        public IList<Roles> GetAllRoles()
        {
            CommandText = "Select * From TB_Roles R Where R.IsDelete = 0 Order By R.ID DESC";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            return table.Rows.Count != 0 ? CollectionHelper.ConvertTo<Roles>(table) : new List<Roles>();
            //return table.Rows.Count != 0 ? CollectionHelper.ConvertTo<Roles>(table) : null;
        }

        public IList<Roles> GetAllRolesIsEnable()
        {
            CommandText = "Select * From TB_Roles R Where R.IsDelete = 0 and R.IsEnable = 1";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            return table.Rows.Count != 0 ? CollectionHelper.ConvertTo<Roles>(table) : new List<Roles>();
            //return table.Rows.Count != 0 ? CollectionHelper.ConvertTo<Roles>(table) : null;

        }

        //Lấy Roles theo ID
        public Roles GetRolesByID( string ID)
        {
            CommandText = "Select * From TB_Roles R Where R.ID = " + ID + " and R.IsDelete = 0";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            return table.Rows.Count != 0 ? CollectionHelper.CreateItem<Roles>(table.Rows[0]) : null;
        }

        //Lấy Roles theo Tên
        public Roles GetRolesByName(string name)
        {
            CommandText = "Select * From TB_Roles R Where R.RoleName = N'" + name + "' and R.IsDelete = 0";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            return table.Rows.Count != 0 ? CollectionHelper.CreateItem<Roles>(table.Rows[0]) : null;
        }

        //Thêm mới Roles :
        // + Nếu role chưa từng tồn tại trong hệ thống : thêm mới bình thừơng
        // + Nếu role nếu role đang tồn tại nhưng ở trạng thái IsDelete hoặc Not IsEnable: thay đổi trạng thái role, không thêm mới

        public bool AddNewRoles(Roles roles)
        {
            return Insert<Roles>("TB_Roles", roles);
            //CommandText = "Insert Into TB_Roles Values('"
            //              + roles.RoleName + "','"
            //              + roles.RoleName.ToLower() + "','"
            //              + roles.Description + "',"
            //              + roles.IsDelete + ","
            //              + roles.IsEnable + ",'"
            //              + roles.CreateDate + "','"
            //              + roles.UpdateDate + "','"
            //              + roles.CreateBy + "','"
            //              + roles.UpdateBy + "')";
            //Commandtype = CommandType.Text;

            //return Save();
        }

        public bool Update(Roles roles)
        {
            return Update("TB_Roles", roles);
        }

        public bool ShiftDelete(string roleID)
        {
            return this.ShiftDelete<Roles>("TB_Roles", roleID);
        }
        

        //lấy roles đã đựoc Xóa(Isdelete = 1) hoặc ở trạng thái 'IsEnable = 0' theo tên role
        public Roles GetRolesDeletedByName(string name)
        {
            CommandText = "Select * From TB_Roles R Where (R.IsDelete = 1 or R.IsEnable = 0) and R.RoleName = N'" + name + "'";
            Commandtype = CommandType.Text;

            DataTable table = GetDataTable();

            if(table.Rows.Count != 0)
            {
                Roles roles = CollectionHelper.CreateItem<Roles>(table.Rows[0]);
                return roles;
            }
            return new Roles();
            
        }

        //Xóa Roles : Chuyển trạng thái IsDelete sang 1
        public bool DeleteRoles(string ID)
        {
            CommandText = "Update TB_Roles R Set R.IsDelete = 1 Where R.ID = " + ID ;
            Commandtype = CommandType.Text;

            return Save();
        }



    }
}
